﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;

namespace ETL.Config
{
	public static class Directories
	{

		public static void CreateSystemDirectories()
		{

			try
			{
				// create outbox directory where the ready to send to FTP files will be saved to
				string outboxdir = GetOutboxDir.FullName;
				if (!Directory.Exists(outboxdir))
					Directory.CreateDirectory(outboxdir);

				// create sent directory where the successfully sent files will be moved to
				string sentdir = GetSentDir.FullName;
				if (!Directory.Exists(sentdir))
					Directory.CreateDirectory(sentdir);

				// create delivery error directory where the unsuccessfully sent files will be moved to
				string delerrordir = GetDeliveryErrorDir.FullName;
				if (!Directory.Exists(delerrordir))
					Directory.CreateDirectory(delerrordir);
			}
			catch (Exception ex)
			{
				throw new Exception(
					"An error was thrown when attempting to create the application base directories (Outbox, Sent and DeliveryError).\n" + 
					"Please make sure you've them configured properly on the application config.\n\n" + 
					"Also make sure you have permissions to access the configured directories." +
					"Exception:\n" + ex.Message);
			}

		}

		public static DirectoryInfo GetOutboxDir
		{
			get
			{
				try
				{
					return new DirectoryInfo(ConfigurationManager.AppSettings["Outbox"]);
				}
				catch (Exception ex)
				{
					throw new Exception("Unable to get Outbox directory path from configuration.\n\nException:\n" + ex.Message);
				}
			}
		}

		public static DirectoryInfo GetSentDir
		{
			get
			{
				try
				{
					return new DirectoryInfo(ConfigurationManager.AppSettings["Sent"]);
				}
				catch (Exception ex)
				{
					throw new Exception("Unable to get Sent directory path from configuration.\n\nException:\n" + ex.Message);
				}
			}
		}

		public static DirectoryInfo GetDeliveryErrorDir
		{
			get
			{
				try
				{
					return new DirectoryInfo(ConfigurationManager.AppSettings["DeliveryError"]);
				}
				catch (Exception ex)
				{
					throw new Exception("Unable to get DeliveryError directory path from configuration.\n\nException:\n" + ex.Message);
				}
			}
		}

	}
}
